package com.example.hibernate.view.util;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.table.AbstractTableModel;

import com.example.hibernate.model.entity.Employee;

public class EmployeeTableModel extends AbstractTableModel {

	private final String[] COLUMN_NAMES = { "ID", "First Name", "Last Name", "Hire Date", "Department" };
	private final Class<?>[] COLUMN_CLASSES = { Long.class, String.class, String.class, Date.class, String.class };

	private List<Employee> employees;

	public EmployeeTableModel() {
		this.employees = new ArrayList<Employee>();
	}

	@Override
	public int getRowCount() {
		return employees.size();
	}

	@Override
	public int getColumnCount() {
		return COLUMN_NAMES.length;
	}

	@Override
	public String getColumnName(int column) {
		return COLUMN_NAMES[column];
	}

	@Override
	public Class<?> getColumnClass(int columnIndex) {
		return COLUMN_CLASSES[columnIndex];
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		Object result = null;
		Employee employee = employees.get(rowIndex);
		switch (columnIndex) {
			case 0:
				result = employee.getId();
				break;
			case 1:
				result = employee.getFirstName();
				break;
			case 2:
				result = employee.getLastName();
				break;
			case 3:
				result = employee.getHireDate();
				break;
			case 4:
				result = employee.getDepartment().getName();
				break;
		}
		return result;
	}

	public void setEmployees(List<Employee> employees) {
		this.employees = employees;
	}

	public List<Employee> getEmployees() {
		return employees;
	}

}
